OpenRoads Designer CONNECT Edition SDK Help

Horizontal alignment creator

Description

  • This is a custom interactive tool for creating alignment using points.

  • The user selects a feature definition and name, places data points, and then right-clicks to complete the command which creates a complex horizontal alignment with the given information.

  • The HorizontalAlignmentCreator class which extends DgnElementSetTool which handles different events to interact with UI, the HorizontalAlignmentCreator class overrides the events here in this tool.

Remarks

  • This sample code is a part of ManagedSDKExample which you get with SDK installation under "examples" section in SDK installation directory.

  • If you encounter any error while using DgnElementSetTool class, make sure to add a reference to Bentley.DgnDisplayNet.dll by selecting Project > Add Reference or change the projects.csproj file to add reference to this dll .

  • The default dll location will be "C:\Program Files\Bentley\OpenRoads Designer CE 10.11\OpenRoadsDesigner\Bentley.DgnDisplayNet.dll".

Source Code

//Required References
using System.Collections.Generic;
using Bentley.DgnPlatformNET;
using Bentley.DgnPlatformNET.Elements;
using Bentley.CifNET.LinearGeometry;
using Bentley.GeometryNET;
using Bentley.MstnPlatformNET;
using Bentley.CifNET.GeometryModel.SDK.Edit;
using Bentley.CifNET.SDK.Edit;
using Bentley.CifNET.GeometryModel.SDK;

namespace ManagedSDKExample
    {
    class HorizontalAlignmentCreator : DgnElementSetTool
        {
        internal static DgnModel activeModel = Bentley.MstnPlatformNET.Session.Instance.GetActiveDgnModel();
        internal static ModelInfo info = activeModel.GetModelInfo();
        private double UOR_TO_MASTER = 1.0 / info.UorPerMeter;

        List<DPoint3d> m_points = new List<DPoint3d>();
        string m_featureDef;
        string m_featureName;

        /*----------------------------------------------------------------------------------------------**/
        /* Write Function | The user is first prompted to select a feature definition and name it. The user
         *                  is then prompted to place data points. As the user places points, they are temporarily
         *                  linked by a visible line. When the user right-clicks, a complex horizontal alignment
         *                  is created from the points.
        /*--------------+---------------+---------------+---------------+---------------+----------------*/
        internal void CreateAlignmentFromPoints()
            {
            List<LinearElement> lines = new List<LinearElement>();

            if (m_points.Count < 2)
                {
                return;
                }

            //adjusts x and y values
            DPoint3d startPoint = new DPoint3d(m_points[0]);
            startPoint.X *= UOR_TO_MASTER;
            startPoint.Y *= UOR_TO_MASTER;
            startPoint.Z *= UOR_TO_MASTER;
            for (int i = 1; i < m_points.Count; i++)
                {
                DPoint3d endPoint = new DPoint3d(m_points[i]);
                endPoint.X *= UOR_TO_MASTER;
                endPoint.Y *= UOR_TO_MASTER;
                endPoint.Z *= UOR_TO_MASTER;

                //creates a line between each consecutive pair of points
                Line line = Line.Create1(startPoint, endPoint);
                lines.Add(line);
                startPoint = endPoint;
                }

            LinearComplex complexAlign = LinearComplex.Create1(lines.ToArray(), false, false, 0.001);

            ConsensusConnectionEdit con = ConsensusConnectionEdit.GetActive();

            GeometricModel gm = con.GetOrCreateGeometricModel();

            if (gm == null)
                {
                con.Close();
                con.Dispose();
                return;
                }

            con.StartTransientMode();
            Alignment al = gm.CreateAlignmentByLinearElement(complexAlign, true);
            con.PersistTransients();

            //sets feature definition and feature name
            if ( m_featureName != null && m_featureName != string.Empty && m_featureDef != null && m_featureDef != string.Empty )
                {
                al.SetFeatureDefinition(m_featureDef, m_featureName);
                }
            else if (m_featureDef != null && m_featureDef != string.Empty)
                {
                al.SetFeatureDefinition(m_featureDef);
                }
            }

       private void GetFeatureDefinition()
        {     
            m_featureDef = "Alignment\\Geom_Baseline";
            m_featureName = "GeomBL";
        }

        protected override void OnPostInstall()
            {
            NotificationManager.OutputPrompt("Select first data point.");
            BeginDynamics();
            GetFeatureDefinition();
            }
        
        //adds points on click
        protected override bool OnDataButton(DgnButtonEvent ev)
            {
            if (m_points.Count == 0)
                {
                BeginDynamics();
                }

            m_points.Add(ev.Point);
            NotificationManager.OutputPrompt("Select next data point or right click to complete the command.");
            return true;
            }

        //calls command on reset (right click)
        protected override bool OnResetButton(DgnButtonEvent ev)
            {
            CreateAlignmentFromPoints();
            EndDynamics();
            m_points.Clear();
            NotificationManager.OutputPrompt("Command complete. Select first data point or pick element selection tool to exit command.");
            return false;
            }

        //allows preview lines to be drawn
        protected override void OnDynamicFrame(DgnButtonEvent ev)
            {
            RedrawElems redraw = new RedrawElems();
            redraw.DrawMode = DgnDrawMode.TempDraw;
            redraw.DrawPurpose = DrawPurpose.Dynamics;
            redraw.SetViewport(ev.Viewport);

            if (m_points.Count > 1)
                {
                LineStringElement lse = new LineStringElement(Session.Instance.GetActiveDgnModel(), null, m_points.ToArray());
                redraw.DoRedraw(lse);
                }
            }

        protected override void OnRestartTool()
            {
            InstallNewInstance();
            }

        public override StatusInt OnElementModify(Element element)
            {
            return StatusInt.Error;
            }

        public static void InstallNewInstance()
            {
            HorizontalAlignmentCreator tool = new HorizontalAlignmentCreator();
            tool.InstallTool();
            }
        }
    }